import { defineStore } from "pinia";
import { getDir, searchByName } from "../../api/index";
import { FileItf } from "../../interface/index";
import { getFavourite } from "@/api/favourite";
import { getCollect } from "../../api/collect";
import { nextTick } from "vue"

// 最大缓存数量
const maxCacheLength = 10;

interface FolderVideoImgItf {
    id?: number;
    imgs: FileItf[];
    videos: FileItf[];
    folder: FileItf[];
    currentFolderName: string;
    currentPath: string;
}

export const pathStore = defineStore("file", {
    state() {
        let imgs: FileItf[] = [];
        let videos: FileItf[] = [];
        let folders: FileItf[] = [];
        // 图片展示列表
        let srcList: string[] = [];
        let currentFolderName: string = "";
        let currentPath: string = "";
        let FolderVideoImgList: FolderVideoImgItf[] = [];
        let pageHeight: number = 0
        return {
            imgs,
            videos,
            folders,
            srcList,
            currentFolderName,
            currentPath,
            FolderVideoImgList,
            pageHeight
        };
    },
    getters: {
        getSrcList(state) {
            return state.srcList;
        },
    },
    actions: {
        /**
         * @description: 点击图片之后，往展示列表里面插入图片路径, 并返回下标
         * @param {string} path
         * @return {*}
         * @author: tao wenbo 2419421809@qq.com
         */
        addSrcList(path: string): number {
            if (!this.srcList.includes(path)) {
                this.srcList.push(path);
                return this.srcList.length - 1;
            }
            return this.srcList.findIndex((item) => item === path);
        },

        async getDirById(id: number) {
            let index: number = this.FolderVideoImgList.findIndex( item => item.id === id )

            if (index === -1) {
                let { data } = await getDir(id);
                data.id = id
                if( this.FolderVideoImgList.length > maxCacheLength ){
                    this.FolderVideoImgList.shift()
                }
                this.FolderVideoImgList.push( data )
            }else{
                let copy = this.FolderVideoImgList[index]
                this.FolderVideoImgList.splice(index, 1)
                this.FolderVideoImgList.push( copy )
            }
            index = this.FolderVideoImgList.length - 1
            let data = this.FolderVideoImgList[index]
            this.imgs = data.imgs;
            this.srcList = [];
            this.videos = data.videos;
            this.folders = data.folder;
            this.currentFolderName = data.currentFolderName;
            this.currentPath = data.currentPath;
            await nextTick()
            this.pageHeight = document.documentElement.scrollHeight
        },

        /**
         * @description: 通过输入搜索相关DIR
         * @param {string} searchText
         * @return {*}
         * @author: tao wenbo 2419421809@qq.com
         */
        async searchBySerText(searchText: string) {
            let { data } = await searchByName(searchText);
            this.imgs = [];
            this.videos = [];
            this.folders = data.folder;
            this.currentPath = "";
            this.srcList = [];
            this.currentFolderName = data.currentFolderName;
        },
        /**
         * @description: 获取喜欢列表---喜欢的是单个视频
         * @return {*}
         * @author: tao wenbo 2419421809@qq.com
         */
        async getFavourites() {
            let res = await getFavourite(-1);
            this.videos = res.data;
            this.imgs = [];
            this.folders = [];
            console.log(res);
        },
        /**
         * @description: 获取收藏列表---收藏的是文件夹
         * @return {*}
         * @author: tao wenbo 2419421809@qq.com
         */
        async getCollects() {
            let res = await getCollect(-1);
            this.videos = [];
            this.imgs = [];
            this.folders = res.data;
            console.log(res);
        },
    },
});
